<?php

namespace api\modules\v1\controllers\patient;

use common\helpers\CommonHelper;
use services\patient\PatientService;
use services\spoker\SpokerService;
use Yii;
use api\controllers\OnAuthController;
use yii\db\Exception;

class PatientController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/patient/patient/index 客户列表
     * @apiGroup Patient
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {string} keywords                      搜索
     * @apiParam {int} spoker_id                        代言人ID
     * @apiParam {int} data_status                      数据状态【1登记、2预约、3到院】
     * @apiParam {int} eky_repeat                       数据重复
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Object}  data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               列表ID
     * @apiSuccess {String}  data.list.name             姓名
     * @apiSuccess {String}  data.list.mobile           手机号
     * @apiSuccess {String}  data.list.type_str         客户类型
     * @apiSuccess {String}  data.list.data_status_str  数据状态
     * @apiSuccess {String}  data.list.arrive_num       到店数
     * @apiSuccess {String}  data.list.sale_money       销售额
     * @apiSuccess {String}  data.list.eky_repeat       重复状态【1重复、0不重复】
     * @apiSuccess {String}  data.list.created_at_str               创建日期
     * @apiSuccess {String}  data.list.arrive_num                   到店次数
     * @apiSuccess {String}  data.list.sale_money                   消费金额
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $service = new PatientService();
        $list['list'] = $service->patientList($params, $page, $pageSize);
        $list['condition'] = [
            'spoker_arr' => SpokerService::getListCondition(),
            'status_arr' => PatientService::getDataStatusCondition(),
        ];
        return $list;
    }

    /**
     * @throws Exception
     * @api {post} /v1/patient/patient/connect 关联
     * @apiGroup Patient
     * @apiName connect
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                  客户ID
     *
     * @apiSuccess {Number} data
     */
    public function actionConnect()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '请选择关联的客户');
        }
        $service = new PatientService();
        return $service->connect($params);
    }

    /**
     * @throws Exception
     * @api {post} /v1/patient/patient/delete 删除
     * @apiGroup Patient
     * @apiName delete
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                  客户ID
     *
     * @apiSuccess {Number} data
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '请选择客户');
        }
        $service = new PatientService();
        return $service->delete($params);
    }

    /**
     * @api {get} /v1/patient/patient/info  患者信息
     * @apiName info
     * @apiGroup Patient
     * @apiUse CommonParam
     *
     * @apiParam {String} id                    患者ID
     *
     * @apiSuccess {Object}  data                   返回数据
     * @apiSuccess {Number}  data.id                ID
     * @apiSuccess {String}  data.name              姓名
     * @apiSuccess {Number}  data.mobile            手机号
     * @apiSuccess {String}  data.type_str          类型
     * @apiSuccess {Number}  data.discount_rate     折扣
     * @apiSuccess {String}  data.remark            备注
     * @apiSuccess {Number}  data.order_id          预约ID【=0 新增、>0 编辑】
     * @apiSuccess {Number}  data.arrive_num        到店数
     * @apiSuccess {Number}  data.sale_money        消费额
     * @apiSuccess {String}  data.created_at_str    创建时间
     * @apiSuccess {String}  data.created_at_str    创建时间
     * @apiSuccess {Array}   data.record_list       消费、预约记录
     * @apiSuccess {Number}  data.record_list.id    ID
     * @apiSuccess {Number}  data.record_list.type  类型【1消费、2预约】
     * @apiSuccess {String}  data.record_list.shop_name  门店
     * @apiSuccess {Array|String}   data.record_list.treatment_project  项目【type=1数组、type=2字符串】
     * @apiSuccess {String}  data.record_list.created_at_str  时间
     * @apiSuccess {Array}   data.record_list.actual_price  支付金额【type=1】
     * @apiSuccess {String}  data.record_list.consultant_name  咨询【type=1】
     * @apiSuccess {String}  data.record_list.doctor_name  医生【type=1】
     * @apiSuccess {String}  data.record_list.remark    备注【type=2】
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        $service = new PatientService();
        // 查询结果
        return $service->getInfo((int)$id);
    }
}
